#!/usr/bin/env python

import sys
import os
from threading import Thread
import locale, gettext

if os.path.exists( "./lib" ):
	sys.path.append( "./lib" )

import translate
from translate import google_translate,bing_translate

import pygtk
pygtk.require('2.0')
import gtk

APP="bgTranslator"
DIR="/usr/share/locale"

def tr( s ):
	return s

# set the locale to LANG, or the user's default
locale.setlocale(locale.LC_ALL, '')
locale.bindtextdomain( APP, DIR )
gettext.bindtextdomain( APP, DIR )
locale.textdomain( APP )
try:
	lang=gettext.translation( APP, DIR )
	lang.install( APP, DIR )
	_=lang.gettext
except:
	# fallback
	_=tr
	pass

class TranslateThread( Thread ):
	def __init__(self, str, func, widget, button ):
		self.str = str
		self.func = func
		self.widget = widget 
		self.button = button
		Thread.__init__(self)

	def run( self ):
		tb = gtk.TextBuffer()
		errmsg = _("error")
		try:
			old_text = self.button.get_label()
			self.button.set_label( _("Connecting...") )
			tb.set_text( self.func( self.str ) )
			self.widget.set_buffer( tb )
			self.button.set_label( old_text )
		except:
			tb.set_text( errmsg )
			self.widget.set_buffer( tb )

class MainWindow:
	def __init__(self):
		self.InitializeComponent()
		self.mapping = [
			( self.txtGoogleResult, google_translate.GoogleTranslate(), self.btnCopy1 ),
			( self.txtBingResult, bing_translate.BingTranslate(), self.btnCopy2 )
		]

	def InitializeComponent(self):
		self.ui = None
		try:
			self.ui = gtk.Builder()
			ui_file = os.path.join(
					os.path.dirname(translate.__file__),
					"ui","translator.xml" )
			self.ui.add_from_file( ui_file )
		except Exception as (err):
			print err
		self.window = self.ui.get_object("window1")
		self.txtOrig = self.ui.get_object("txtOrig")
		self.txtGoogleResult = self.ui.get_object("txtGoogleResult")
		self.txtBingResult = self.ui.get_object("txtBingResult")
		self.btnTranslate = self.ui.get_object("btnTranslate")
		self.btnCopy1 = self.ui.get_object("btnCopy1")
		self.btnCopy2 = self.ui.get_object("btnCopy2")
		self.ui.connect_signals( self )
		self.clipboard = gtk.Clipboard()
		self.window.set_default( self.btnTranslate )
		self.window.show()
	
	def on_btnTranslate_clicked(self, widget):
		self.btnTranslate.set_sensitive( False )
		tb = self.txtOrig.get_buffer()
		str = tb.get_text( tb.get_start_iter(), tb.get_end_iter() )
		threads = []
		for t in self.mapping:
			w, o, b = t
			thread = TranslateThread( str, o.translate, w, b )
			thread.start()
			threads.append( thread )
		for thread in threads:
			thread.join()
		self.btnTranslate.set_sensitive( True )

	def on_window1_delete_event(self, widget, data=None):
		gtk.main_quit()

	def on_copy_clicked( self, widget ):
		tb = widget.get_buffer()
		text = tb.get_text( tb.get_start_iter(), tb.get_end_iter() )
		self.clipboard.set_text( text )

	def on_btnCopy1_clicked( self, widget ):
		self.on_copy_clicked( self.txtGoogleResult )

	def on_btnCopy2_clicked( self, widget ):
		self.on_copy_clicked( self.txtBingResult )

	def on_btnPasteFromClipboard_clicked( self, widget ):
		def clipboard_callback(clipboard, text, data):
			tb = gtk.TextBuffer()
			tb.set_text(text)
			self.txtOrig.set_buffer( tb )
		self.clipboard.request_text( clipboard_callback )

# we start the app like this...
if __name__ == "__main__":
	window=MainWindow()
	gtk.main()

